package com.wbf.demo.domain;

import lombok.Data;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * @program: security_demo
 * @description:
 * @author: guangdao
 * @create: 2022-04-14 19:20
 **/
@Data
public class Subject implements UserDetails {
    private static final long serialVersionUID = 1L;
    private Long id;
    /**
     * 用户名
     */
    private String username;
    /**
     * 密码
     */
    private String password;
    /**
     * 昵称
     */
    private String nickName;
    /**
     * 手机号
     */
    private String mobile;
    /**
     * 是否删除，0有效 1删除
     */
    private Integer isDelete;
    /**
     * 角色列表
     */
    private List<Role> roleList;
    /**
     * 去重后的菜单列表
     */
    private List<Menu> menuList;

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        List<GrantedAuthority> authorities = new ArrayList<>();
        menuList.forEach(menu -> {
            // security权限注解会默认增加前缀"ROLE_"，所以这里需要加上默认前缀才能正确对比
            authorities.add(new SimpleGrantedAuthority("ROLE_" + menu.getPermissions()));
        });
        return authorities;
    }

    @Override
    public String getPassword() {
        return this.password;
    }

    @Override
    public String getUsername() {
        return this.username;
    }

    @Override
    public boolean isAccountNonExpired() {
        return false;
    }

    @Override
    public boolean isAccountNonLocked() {
        return false;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return false;
    }

    @Override
    public boolean isEnabled() {
        return isDelete == 0 ? true : false;
    }
}
